// pages/home/dec_budget/detail/detail.js
import { _order_adorn_service } from '../../../../utils/qiaofinn'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    mobile: '',
    houseArea: 0,
    houseModelIndex: [],
    dec_type: [{ name: '简装', multiple: [4.1, 5.9] }, { name: '精装', multiple: [4.8, 5.2] }, { name: '豪装', multiple: [6 + 0.1] }],
    dec_type_index: 0,
    // 二室一厅一卫
    spaceList: [{ name: '卧室', price: '' }, { name: '客厅' }, { name: '卫生间' }, { name: '厨房' }, { name: '水电' }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const houseArea = options.houseArea
    let houseMode = '一室一厅一卫'
    let spaceList = [0.2036, 0.2546, 0.1561, 0.1661, 0.1059]
    if (houseArea > 50 && houseArea < 120) {
      houseMode = '二室一厅一卫'
      spaceList = [0.2725, 0.2468, 0.1758, 0.1624, 0.1335]
    }
    else if (houseArea >= 120) {
      houseMode = '三室二厅二卫'
      spaceList = [0.2674, 0.2108, 0.1784, 0.1647, 0.1423]
    }
    // 6+1.35
    var increase = 0.007 * houseArea

    if (houseArea > 100) {
      increase = -0.007 * houseArea + 1.4
    }
    const houseArea_price = houseArea / 100

    let total_price_min = (4 + increase) * houseArea_price
    let total_price_max = (5 + 1 - increase) * houseArea_price
    let average_price = (total_price_min + total_price_max) / 2

    let item_price1 = {
      name: '简装', multiple: [total_price_min.toFixed(1), total_price_max.toFixed(1)],
      people_price: [(total_price_min * 0.6).toFixed(1), (total_price_max * 0.57).toFixed(1)],
      material_price: [(total_price_min * 0.4).toFixed(1), (total_price_max * 0.43).toFixed(1)],
      spaceList: [
        { name: '卧室', price: (average_price * spaceList[0]).toFixed(2) },
        { name: '客厅', price: (average_price * spaceList[1]).toFixed(2) },
        { name: '卫生间', price: (average_price * spaceList[2]).toFixed(2) },
        { name: '厨房', price: (average_price * spaceList[3]).toFixed(2) },
        { name: '阳台过道', price: (average_price * spaceList[4]).toFixed(2) }]
    }
    total_price_min = (5.1 + increase) * houseArea_price
    total_price_max = (6.1 + 1 - increase) * houseArea_price
    average_price = (total_price_min + total_price_max) / 2

    let item_price2 = {
      name: '精装', multiple: [total_price_min.toFixed(1), total_price_max.toFixed(1)],
      people_price: [(total_price_min * 0.51).toFixed(1), (total_price_max * 0.47).toFixed(1)],
      material_price: [(total_price_min * 0.49).toFixed(1), (total_price_max * 0.53).toFixed(1)],
      spaceList: [{ name: '卧室', price: (average_price * spaceList[0]).toFixed(2) },
      { name: '客厅', price: (average_price * spaceList[1]).toFixed(2) },
      { name: '卫生间', price: (average_price * spaceList[2]).toFixed(2) },
      { name: '厨房', price: (average_price * spaceList[3]).toFixed(2) },
      { name: '阳台过道', price: (average_price * spaceList[4]).toFixed(2) }]
    }
    total_price_min = (6 + increase) * houseArea_price
    average_price = total_price_min

    let item_price3 = {
      name: '豪装', multiple: [total_price_min.toFixed(1), total_price_max.toFixed(1)],
      people_price: [(total_price_min * 0.45).toFixed(1)],
      material_price: [(total_price_min * 0.55).toFixed(1)],
      spaceList: [{ name: '卧室', price: (average_price * spaceList[0]).toFixed(2) },
      { name: '客厅', price: (average_price * spaceList[1]).toFixed(2) },
      { name: '卫生间', price: (average_price * spaceList[2]).toFixed(2) },
      { name: '厨房', price: (average_price * spaceList[3]).toFixed(2) },
      { name: '阳台过道', price: (average_price * spaceList[4]).toFixed(2) }]
    }

    this.setData({
      houseArea: houseArea,
      houseMode: houseMode,
      dec_type: [item_price1, item_price2, item_price3]
    })
  },
  orderServiceTap: function () {
    const { name, mobile } = this.data
    let regMobile = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (name !== '' && mobile !== '' && name.length < 5 && mobile.length === 11 && regMobile.test(mobile)) {
      _order_adorn_service(name, mobile, '', 'adorn', res => {
        if (res.status) {
          wx.showModal({
            title: '提示',
            content: '预约成功，稍后会有工作人员与你联系。',
            showCancel: false
          })
          this.setData({
            name:'',
            mobile:''
          })
        }
      })
    } else {
      wx.showToast({
        title: '请输入正确的手机号或者姓名',
        icon: 'none'
      })
    }
  },
  // 装修类型选择
  decTypeTabSelect: function (res) {
    this.setData({
      dec_type_index: res.target.dataset.id
    })
  },
  // 绑定姓名和手机号
  nameInputBlurTap: function (res) {
    this.setData({
      name: res.detail.value
    })
  },
  mobileInputBlurTap: function (res) {
    this.setData({
      mobile: res.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})